<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小账单物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82882996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\property\work;

use think\App;
use app\controller\property\Base;
use app\validate\work\WorkReportValidate;
use app\common\repositories\work\WorkReportRepository;
use app\common\repositories\approval\ApprovalConfigRepository;
use app\common\repositories\approval\ApprovalReceivesRepository;

class WorkReport extends Base
{

    protected $repository;

    public function __construct(App $app, WorkReportRepository $repository)
    {
        parent::__construct($app);

        $this->repository = $repository;
        $this->assign([
            'addAuth' => property_auth('propertyWorkReportSubmitAdd'),
            'editAuth' => property_auth('propertyWorkReportSubmitEdit'),
            'delAuth' => property_auth('propertyAWorkReportSubmitDel'),
            'detailAuth' => property_auth('propertyWorkReportSubmitDetail'),
            'readAuth' => property_auth('propertyWorkReportReportReadInfo'),
        ]);
    }


    public function workbenchIndex()
    {
        return $this->fetch('work/workbench_index');
    }

    /**
     *  我提交的报告列表
     * @return string|\think\response\Json
     * @throws \Exception
     *
     * @author Amos
     * 2024-05-21 21:03
     */
    public function isubmitList()
    {
        if ($this->request->isAjax()) {
            $where = $this->request->param([
                'report_type' => '',
                'keywords' => ''
            ]);
            [$page, $limit] = $this->getPage();
            $where['user_id'] = $this->request->userId;
            $data = $this->repository->getpropertyList($where, $page, $limit, $this->request->propertyId);
            return json()->data(['code' => 0, 'data' => $data['list'], 'count' => $data['count']]);
        }
        /** @var ApprovalConfigRepository $configRepository */
        $configRepository = app()->make(ApprovalConfigRepository::class);
        $reportTypeData = $configRepository::FLOW_TYPE_A;
        return $this->fetch('work/report/isubmit_list', [
            'reportTypeData' => $reportTypeData
        ]);
    }

    /**
     * 提交报告
     * @return string|\think\response\Json|\think\response\View
     * @throws \Exception
     *
     * @author Amos
     * 2024-05-22 11:39
     */
    public function submitAdd()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'config_id' => '',
                'report_content' => '',
            ]);
            $param['user_id'] = $this->request->userId;
            validate(WorkReportValidate::class)->scene('add')->check($param);
            $res = $this->repository->addInfo($param, $this->request->propertyId, $this->request->userId);
            if ($res) {
                return $this->success('添加成功');
            } else {
                return $this->error('添加失败');
            }
        } else {
            /** @var ApprovalConfigRepository $approvalConfigRepository */
            $approvalConfigRepository = app()->make(ApprovalConfigRepository::class);
            $configData = $approvalConfigRepository->getConfigData($this->request->propertyId, ['is_show' => 1, 'is_type' => 1]);
            return $this->fetch('work/report/submit_add', [
                'configData' => $configData
            ]);
        }
    }

    /**
     * 编辑本人提交的报告
     * @return string|\think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-05-23 10:13
     */
    public function edit()
    {
        $id = (int)$this->request->param('id');
        if (!$id) {
            return $this->error('参数错误');
        }
        $info = $this->repository->get($id);
        if (!$info) {
            return $this->error('信息错误');
        }
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'report_content' => '',
            ]);
            if ($info['user_id'] != $this->request->userId) {
                return $this->error('非法操作');
            }
            validate(WorkReportValidate::class)->scene('edit')->check($param);
            try {
                $res = $this->repository->editInfo($info, $param, $this->request->userId);
                if ($res) {
                    return $this->success('修改成功');
                } else {
                    return $this->error('修改失败');
                }
            } catch (\Exception $e) {
                exception_log('编辑工作报告失败', $e);
                return $this->error('网络错误');
            }
        } else {
            return $this->fetch('work/report/submit_edit', [
                'info' => $info,
            ]);
        }
    }


    public function del()
    {
        $ids = (array)$this->request->param('ids');
        try {
            $data = $this->repository->batchDelete($ids, $this->request->userId);
            if ($data) {
                return $this->success('删除成功');
            } else {
                return $this->error('删除失败');
            }
        } catch (\Exception $e) {
            exception_log('删除企业失败', $e);
            return $this->error('网络失败');
        }
    }

    public function detail()
    {
        $id = (int)$this->request->param('report_id');
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        $info = $this->repository->getDetail($id);
        if ($info['id'] <= 0) {
            return $this->error('信息不存在');
        }
        return $this->fetch('work/report/submit_detail', [
            'info' => $info,
        ]);
    }


    /**
     * 汇报至我的
     * @return string|\think\response\Json
     * @throws \Exception
     *
     * @author Amos
     * 2024-05-21 21:03
     */
    public function reportToMe(ApprovalReceivesRepository $repository)
    {
        if ($this->request->isAjax()) {
            $where = $this->request->param([
                'flow_type' => '',
                'is_check' => '',
                'keywords' => ''
            ]);
            [$page, $limit] = $this->getPage();
            $where['is_type'] = 1;
            $where['present_uid'] = $this->request->userId;
            $data = $repository->getpropertyList($where, $page, $limit, $this->request->propertyId);
            return json()->data(['code' => 0, 'data' => $data['list'], 'count' => $data['count']]);
        }
        /** @var ApprovalConfigRepository $configRepository */
        $configRepository = app()->make(ApprovalConfigRepository::class);
        $reportTypeData = $configRepository::FLOW_TYPE_A;
        return $this->fetch('work/report/reportToMe', [
            'reportTypeData' => $reportTypeData
        ]);
    }

    /**
     * 阅读报告
     * @param ApprovalReceivesRepository $repository
     * @return string|\think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-05-24 07:46
     */
    public function readReport(ApprovalReceivesRepository $repository)
    {
        $receivesId = (int)$this->request->param('receives_id');
        if ($receivesId <= 0) {
            return $this->error('参数错误');
        }
        $receivesInfo = $repository->get($receivesId);
        if (!$receivesInfo) {
            return $this->error('信息不存在');
        }
        if ($receivesInfo['present_uid'] != $this->request->userId) {
            return $this->error('非法操作');
        }
        $reportInfo = $this->repository->get($receivesInfo['source_id']);
        if (!$reportInfo) {
            return $this->error('信息不存在');
        }
        ## 调整阅读
        if ($receivesInfo['is_check'] == 1) {
            $receivesInfo->is_check = 2;
            $receivesInfo->save();
        }
        return $this->fetch('work/report/read_detail', [
            'info' => $reportInfo,
        ]);
    }

}